package com.sk.skplanet.commute.fragments;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.sk.skplanet.commute.R;
import com.sk.skplanet.commute.daofactory.LocalDaoFactory;

public class BusFirstSectionFragment extends Fragment {
	private List<String> busNameList;
	private List<String> busTypeList;
	private List<String> busIdList;
	private EditText searchTextBus;
	private ListView listView;
	private MyArrayAdapter adapter;

	private OnBusItemSelectedListener mListener;

	public BusFirstSectionFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		busNameList = new ArrayList<String>();
		busTypeList = new ArrayList<String>();
		busIdList = new ArrayList<String>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_bus_first_section, container,
				false);
		
		searchTextBus = (EditText)view.findViewById(R.id.searchTextBus);
		searchTextBus.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				search(s.toString());
			}
		});
		
		listView = (ListView)view.findViewById(R.id.busSearchList);
		adapter = new MyArrayAdapter(getActivity());
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView av, View v, int i, long l) {
				mListener.onBusItemSelected("140");
			}
		});
		
		return view;
	}
	
	private void search(String arg) {
		busNameList.clear();
		busTypeList.clear();
		busIdList.clear();
		
		if(arg.length() > 0) {
			Cursor cursor = LocalDaoFactory.selectBusByBusName(arg);
					
			if(cursor != null && cursor.getCount() != 0) {
				cursor.moveToFirst();
				
				while(!cursor.isAfterLast()) {
					busNameList.add(cursor.getString(cursor.getColumnIndex("busNumberKor")));
					busTypeList.add(cursor.getString(cursor.getColumnIndex("busType")));
					busIdList.add(cursor.getString(cursor.getColumnIndex("busId")));
					
					cursor.moveToNext();
				}
			} else {
				// 검색결과 없음처리
			}
		}
		
		adapter.notifyDataSetChanged();
	}

	public void onButtonPressed(String busName) {
		if (mListener != null) {
			mListener.onBusItemSelected(busName);
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (OnBusItemSelectedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnFragmentInteractionListener");
		}
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mListener = null;
	}

	public interface OnBusItemSelectedListener {
		public void onBusItemSelected(String busName);
	}
	
	private class MyArrayAdapter extends ArrayAdapter {
		Context context;
		
		MyArrayAdapter(Context context) {
			super(context, R.layout.listview_search_bus_info, busNameList);
			
			this.context = context;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			
			View row = (View)inflater.inflate(R.layout.listview_search_bus_info, null);
			
			TextView busName = (TextView)row.findViewById(R.id.listview_search_bus_text_busno);
			TextView busType = (TextView)row.findViewById(R.id.listview_search_bus_text_bustype);
			
			row.setTag(busIdList.get(position));
			busName.setText(busNameList.get(position));
			
			if(busTypeList.get(position).equals("간선")) {
				busName.setTextColor(Color.parseColor("#0000FF"));
			} else if(busTypeList.get(position).equals("지선")) {
				busName.setTextColor(Color.parseColor("#008000"));
			} else if(busTypeList.get(position).equals("순환")) {
				busName.setTextColor(Color.parseColor("#FFA500"));
			} else if(busTypeList.get(position).equals("광역")) {
				busName.setTextColor(Color.parseColor("#FF0000"));
			}
			
			busType.setText(busTypeList.get(position));
			
			return row;
		}
	}

}
